package ai.plantdata.config;

import ai.plantdata.constant.DbTypeEnum;
import lombok.Getter;

import java.util.TimeZone;
import java.util.function.BiFunction;

@Getter
public class ConnectionDTO {
    private String driverClass;
    private BiFunction<String, String, String> urlFactory;

    private DbTypeEnum dbType;

    private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
    private static final String ORACLE_DRIVER = "oracle.jdbc.OracleDriver";

    public static final ConnectionDTO MYSQL;
    public static final ConnectionDTO ORACLE;

    static {
        MYSQL = new ConnectionDTO();
        MYSQL.driverClass = MYSQL_DRIVER;
        MYSQL.dbType = DbTypeEnum.MYSQL;
        final String timeZone = TimeZone.getDefault().getID();
        MYSQL.urlFactory = (address, dbName) -> "jdbc:mysql://" + address + "/" + dbName + "?" +
                "serverTimezone=" + timeZone + "&characterEncoding=utf8&useUnicode=true&useSSL=false&allowPublicKeyRetrieval=true";

        ORACLE = new ConnectionDTO();
        ORACLE.dbType = DbTypeEnum.ORACLE;
        ORACLE.driverClass = ORACLE_DRIVER;
        ORACLE.urlFactory = (address, dbName) -> {
            return "jdbc:oracle:thin:@" + address;
        };
    }


}